
<script>
var previousSelectedVersion = 0;
var previousProtectChoice = -1;
var guid="<?php echo $guid; ?>";

function clickOnDelete()
{
	var message='';
	if(previousSelectedVersion==0)
	{
		message+='Please select a version first.';
	}
	else
	{
		message+='Are you sure to delete version '+previousSelectedVersion+' of item:'+guid+'?';
	}
	var toDelete=window.confirm(message);
	if(!toDelete||previousSelectedVersion==0)
		return false;//decline or didn't select a row
	else
		return true;//submit request
}

function clickOnView()
{
	if(previousSelectedVersion==0)
	{
		alert('Please select a version first.');
		return false;
	}
	return true;
}

function checkProtection()
{
	if(previousProtectChoice==1)
	{
		$("#hash-input-dialog").dialog("open");
		return true;
	}
	return false;
}

function afterDelete(data)
{
	var message=data[0];
	var records=data[1];
	alert(message);
	if(records==0)
	{
		var mes="all the versions of item:"+guid+" are deleted, the address\r\n";
		mes+="'"+window.location.href+"'\r\n";
		mes+="is no longer available.";
		alert(mes);
		window.location.href="<?php echo Yii::app()->request->baseUrl; ?>";
	}
	else if(records>0)
	{
		$.fn.yiiGridView.update("versionTable");
		previousSelectedVersion=0;//reset
		previousProtectChoice=-1;
	}
}

$(document).ready(function ()
{
	$('#delete-button').click(function (){
		if(clickOnDelete())
		{
			if(!checkProtection())
			{
				$.ajax({
			    url: '<?php echo CController::createUrl('manage/deleteVersion');?>',
			    type: 'post',
			    dataType: 'json',
			    data: {version:previousSelectedVersion, guid:guid},
			    success: afterDelete
     			});
			}
		}
   	});
	$('#view-button').click(function(){
		if(clickOnView())
		{
			window.location.href='<?php echo Yii::app()->request->baseUrl; ?>'+'/manage/view?guid='+guid+'&version='+previousSelectedVersion;
		}
	});
});
</script>

<?php include_once "hashInputDialog.php"; ?>

<?php
function format($size)
{
	$unitArray=array('B', 'KB', 'MB', 'GB');
	$index=0;
	while($size>=1024&&$index<count($unitArray)-1)
	{
		$size=$size/1024;
		$index++;
	}
	return floor($size).''.$unitArray[$index];
}
?>
<?php $form = $this->beginWidget('CActiveForm', array(
		'id'=>'view-list-form',
		'enableClientValidation'=>true,
	//	'enableAjaxValidation'=>true,
		));
?>
<?php
echo "GUID: ".$guid;
CActiveForm::widget('zii.widgets.grid.CGridView', array(
			    	'dataProvider'=>$itemsDataProvider,
			    	'id'=>'versionTable',
			    	'selectableRows'=>1,
			    	'selectionChanged'=>'function(id) {' .
			    		'var columnID = $.fn.yiiGridView.getSelection(id);'.
			    		'if(columnID[0]!=null) {previousSelectedVersion=columnID[0].split(",")[1]; previousProtectChoice=columnID[0].split(",")[2]}'.
			    	'}',
				/*	'cssFile'=>Yii::app()->request->baseUrl.'/css/gridview.css',*/
					'columns'=>array(
			    		array(
			    			'name'=>'version',
			    			'value'=>'$data->version',
			    		),
			    		array(
			    			'name'=>'symbolic name',
			    			'value'=>'$data->symbolic_name',
			    		),
			    		array(
			    			'name'=>'uploaded starting time',
			    			'value'=>'$data->start_time',
			    		),
			    		array(
			    			'name'=>'uploaded ending time',
			    			'value'=>'$data->end_time',
			    		),
			    		array(
			    			'name'=>'protected by hash',
			    			'value'=>'$data->protected_by_hash? "YES":"NO"',
			    		),
			    	),
				),
				false//captureOutput
			);
?>
<?php
	echo CHtml::button(
		'Add Version', 
		array(
			'onclick'=>'window.location.href="'.Yii::app()->request->baseUrl.'/manage/addVersion?guid='.$guid.'"'	
		)
	);
	echo CHtml::button(
		'Delete Version',
		array(
			'id'=>'delete-button'
		)
	);
	echo CHtml::button(
		'View Details',
		array(
			'id'=>'view-button'
		)
	);
?>
<?php $this->endWidget(); ?>
